#include "CondGotoInstruction.h"
#include "Value.h"
#include <cassert>
#include "Function.h"
#include "Types/VoidType.h"
CondGotoInstruction::CondGotoInstruction(Function * func,
                                         Value * cond,
                                         LabelInstruction * trueLabel,
                                         LabelInstruction * falseLabel)
    : Instruction(func, IRInstOperator::IRINST_OP_BRANCH, VoidType::getType()), condition(cond), trueTarget(trueLabel),
      falseTarget(falseLabel)
{
    addOperand(cond);
    addOperand(trueLabel);
    addOperand(falseLabel);
}

void CondGotoInstruction::toString(std::string & str)
{
    str = "br i1 ";

    // 添加条件值
    if (!condition->getIRName().empty()) {
        str += condition->getIRName();
    } else {
        str += condition->getVal();
    }

    // 添加跳转目标
    str += ", label %" + trueTarget->getIRName();
    str += ", label %" + falseTarget->getIRName();
}